package com.taiyuan.zfmvp.common.annotation;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption: 自定义AOP配置
 * @Date 2022/11/19
 * @return
 */
@Component
@Aspect
public class MyAnnotationAspect {

    private static final Logger logger = LoggerFactory.getLogger(MyAnnotationAspect.class.getName());


    @Pointcut("@annotation(com.taiyuan.zfmvp.common.annotation.MyAnnotation)")
    private void myAnnotation() {
    }

    /**
     * 环绕通知
     */
    @Around("myAnnotation()")
    public void advice(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("执行自定义注解前");

        String obj = JSON.toJSONString(joinPoint.getArgs());
        logger.info("该方法的入参: "+obj);

        //执行到这里走原来的方法
        joinPoint.proceed();
        logger.info("执行自定义注解后");
    }

    @Before("myAnnotation()")
    public void record(JoinPoint joinPoint) {
        logger.info("执行自定义注解Before");
    }

    @After("myAnnotation()")
    public void after() {
        logger.info("执行自定义注解After");

    }

}
